<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item><i class="el-icon-setting"></i> 现场图片管理</el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-date"></i> 新增现场图片</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container category-wrap">
      <el-form :model="categoryForm" label-width="90px" label-position="right">
        <el-row>
          <el-col :span="5">
            <el-form-item label="客户名称">
              <el-select v-model="categoryForm.customerSn" style="float: left" placeholder="请选择">
                <el-option
                  v-for="item in custmoerList"
                  :key="item.customerSn"
                  :label="item.customerName"
                  :value="item.customerSn">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="拍照员工" required label-width="140px" >
              <el-select v-model="categoryForm.employeeId" style="float: left" placeholder="请选择">
                <el-option
                  v-for="item in teamList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地址定位" required label-width="140px">
              <p style="text-align: left">{{position.formattedAddress}}</p>
              <avue-map v-model="position" style="width:200px" :lnglat="lnglat"></avue-map>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="备注">
            <el-input v-model="categoryForm.remarks" type="textarea" style="width: 400px;float: left;" class="form-input"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="图片" required>
              <div v-if="categoryForm.photoList" class="thumbUrl" v-for="(item,index) in categoryForm.photoList">
                <img :src="item.picUrl" />
                <div class="hover_show">
                  <i class="el-icon-delete" @click="handlePictureRemove(index)"></i>
                </div>
              </div>
              <div class="select_img" @click="sel_img()" style="float: left;"><i class="el-icon-plus"></i></div>
              <div v-if="sleImg">
                <el-dialog title="选择图片" :visible.sync="sleImg" style="text-align: left;">
                  <sel-img-photogallery ref="selImg" type="10" @select="onSelectImgs"></sel-img-photogallery>
                </el-dialog>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <div>
            <el-button type="primary" size="medium" @click="save">保存</el-button>
            <el-button type="default" size="medium" @click="close">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {getStore, setStore} from "@/util/store";
  import selImgPhotogallery from '@/components/pds/common/sel_img_photogallery'
  import {
    fetchList,
  } from '@/api/crm/customer'
  import {
    fetchList as teamList,
  } from '@/api/crm/marketemployee' //员工
  import {
    editPicture
  } from '@/api/crm/picturerecord'
  import AvueMap from 'avue-plugin-map'
  import {
    mapGetters
  } from 'vuex'
  import Vue from 'vue'
  import {fetchDict} from '@/util/util';
  export default {
    data() {
      return {
        categoryForm:{
          photoList:[],
          position:''
        },
        sleImg:false,
        //初始化坐标
        lnglat: {
          P: 29.563761,
          R: 106.55046400000003,
        },
        custmoerList:[],
        teamList:[],
        position:{
          formattedAddress:'',
          longitude:'',
          latitude:'',
        },
      }
    },
    components: {  selImgPhotogallery },
    computed: {
      ...mapGetters(['tagWel', 'tagList', 'tag', 'website']),
      tagLen() {
        return this.tagList.length || 0
      }
    },
    mounted() {

    },
    created(){
      Vue.use(AvueMap);
      this.getList();
    },
    methods: {
      //获取销售区域
      getList(page, params) {
        this.tableLoading = true
        fetchList({current: 1, size: 1000}).then(response => {
          this.custmoerList = response.data.data.records
        })
        teamList({current: 1, size: 1000}).then(response => {
          this.teamList = response.data.data.records
        })
      },
      sel_img(){
        this.sleImg = true;
      },
      onSelectImgs(imgs){
        imgs.forEach((item)=>{
          this.categoryForm.photoList.push({id:item.id,picUrl:item.picUrl})
        })
        this.sleImg = false;
      },
      // 删除el-upload的图片
      handlePictureRemove(index) {
        this.categoryForm.photoList.splice(index,1)
      },
      //确定
      save(){
        let tmp = []
        this.categoryForm.photoList.forEach((item)=>{
          tmp.push(item.id);
        })
        this.categoryForm.photoList = tmp;
        let params = this.categoryForm;
        params.position = this.position.longitude + ',' + this.position.latitude
        params.address = this.position.formattedAddress;
        editPicture(params).then(response => {
          if(response.data.code == 0){
            this.$message.success('创建成功')
            let _this = this;
            setTimeout(function () {
              _this.close();
            },2000)
          }else{
            this.$message.success(response.data.msg)
          }
        })
      },
      close(){
        var str_before = location.href.split('#')[1];
        let { tag, key } = this.findTag(str_before);
        this.$store.commit('DEL_TAG', tag);
        this.$router.go(-1);
      },
      findTag(value) {
        let tag, key
        this.tagList.map((item, index) => {
          if (item.value === value) {
            tag = item
            key = index
          }
        })
        return { tag: tag, key: key }
      }
    }
  }
</script>

<style scoped lang="scss">
  .crumbs{
    margin: 20px 10px;
  }
  .thumbUrl{
    width: 150px;
    height: 150px;
    position: relative;
    line-height: 146px;
    float: left;
    margin-right: 15px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid #c0ccda;
  }
  .thumbUrl img{
    width: 100%;
    height: 100%;
  }
  .thumbUrl .hover_show{
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0,0,0,0.5);
    width: 100%;
    height: 100%;
    display: none;
  }
  .thumbUrl:hover .hover_show{
    display: block;
  }
  .thumbUrl .hover_show i{
    color: #fff;
    margin: 0 10px;
    cursor: pointer;
  }
  .select_img{
    width: 148px;
    height: 148px;
    line-height: 146px;
    border: 1px dashed #c0ccda;
    background-color: #fbfdff;
    font-size: 28px;
    color: #8c939d;
    cursor: pointer;
    border-radius: 6px;
  }
</style>


